/**
 * @author yxw
 * date : 2013年11月22日 下午8:04:10 
 */
package com.tzinfo.tzt.activity.view;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.tzinfo.tzt.R;
import com.tzinfo.tzt.activity.MyFabuUpdateActivity;
import com.tzinfo.tzt.util.StringTools;
import com.tzinfo.tzt.view.wheel.adapters.ArrayWheelAdapter;
import com.tzinfo.tzt.view.wheel.adapters.NumericWheelAdapter;
import com.tzinfo.tzt.wheel.OnWheelChangedListener;
import com.tzinfo.tzt.wheel.WheelView;

public class FabuPickViewPopupWindow extends PopupWindow implements
		OnClickListener {
	private Activity context;
	private MyFabuUpdateActivity fabuUpdateActivity;
	private View mMenuView;
	private Button btn_cancel, btn_ok;
	private TextView contentText;
	private LayoutInflater inflater;
	// 标志
	private int flag = 0;

	private int select_shi;
	private int select_ting;
	private int select_wei;

	private String[] directions = { "东", "南", "西", "北", "东南", "西南", "西北", "东北" };
	private String[] areaqys = { "椒江", "黄岩", "路桥", "临海", "温岭", "玉环", "仙居",
			"三门", "天台", "外地" };
	protected WheelView year;
	protected WheelView month;
	protected WheelView day;

	protected WheelView shi;
	protected WheelView ting;
	protected WheelView wei;

	public FabuPickViewPopupWindow(Activity context) {
		super(context);
		this.context = context;
		fabuUpdateActivity = (MyFabuUpdateActivity) context;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	// 显示区域滚轮
	public void showAreaqyPick() {
		flag = 1;
		mMenuView = inflater.inflate(R.layout.layout_wheel_direction, null);
		btn_cancel = (Button) mMenuView.findViewById(R.id.wheel_cancel);
		btn_cancel.setOnClickListener(this);
		btn_ok = (Button) mMenuView.findViewById(R.id.wheel_ok);
		btn_ok.setOnClickListener(this);
		contentText = (TextView) mMenuView.findViewById(R.id.wheel_content);

		final WheelView direction = (WheelView) mMenuView
				.findViewById(R.id.direction);
		direction.setViewAdapter(new ArrayWheelAdapter(context, areaqys));
		// hours.setLabel("hours");

		String textDate = fabuUpdateActivity.text_areaqy.getText()
				.toString();
		if (textDate != null && !"".equals(textDate)) {
			int i = 0;
			for (i = 0; i < areaqys.length; i++) {
				if (areaqys[i].equals(textDate)){
					String s1 = areaqys[i];
					contentText.setText(s1);
					direction.setCurrentItem(i);
					break;
				}
			}
			
		} else {
			// set current time
			contentText.setText(areaqys[0]);
			direction.setCurrentItem(0);
		}

		direction.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// wheel.setLabel(newValue != 1 ? "mins" : "min");
				contentText.setText(areaqys[wheel.getCurrentItem()]);
			}
		});

		this.setContentView(mMenuView);
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		this.setFocusable(true);
		this.setAnimationStyle(R.style.AnimBottom);
		ColorDrawable dw = new ColorDrawable(0x00000000);
		this.setBackgroundDrawable(dw);
		mMenuView.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				int height = mMenuView.findViewById(R.id.wheel_container)
						.getTop();
				int y = (int) event.getY();
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (y < height) {
						dismiss();
					}
				}
				return true;
			}
		});
	}

	// 显示朝向滚轮
	public void showDirectionPick() {
		flag = 2;
		mMenuView = inflater.inflate(R.layout.layout_wheel_direction, null);
		btn_cancel = (Button) mMenuView.findViewById(R.id.wheel_cancel);
		btn_cancel.setOnClickListener(this);
		btn_ok = (Button) mMenuView.findViewById(R.id.wheel_ok);
		btn_ok.setOnClickListener(this);
		contentText = (TextView) mMenuView.findViewById(R.id.wheel_content);

		final WheelView direction = (WheelView) mMenuView
				.findViewById(R.id.direction);
		direction.setViewAdapter(new ArrayWheelAdapter(context, directions));
		// hours.setLabel("hours");

		String textDate = fabuUpdateActivity.text_direction.getText()
				.toString();
		if (textDate != null && !"".equals(textDate)) {
			int i = 0;
			for (i = 0; i < directions.length; i++) {
				if (directions[i].equals(textDate)){
					String s1 = directions[i];
					contentText.setText(s1);
					direction.setCurrentItem(i);
					break;
				}
			}
		} else {
			// set current time
			contentText.setText(directions[0]);
			direction.setCurrentItem(0);
		}

		direction.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// wheel.setLabel(newValue != 1 ? "mins" : "min");
				contentText.setText(directions[wheel.getCurrentItem()]);
			}
		});

		this.setContentView(mMenuView);
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		this.setFocusable(true);
		this.setAnimationStyle(R.style.AnimBottom);
		ColorDrawable dw = new ColorDrawable(0x00000000);
		this.setBackgroundDrawable(dw);
		mMenuView.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				int height = mMenuView.findViewById(R.id.wheel_container)
						.getTop();
				int y = (int) event.getY();
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (y < height) {
						dismiss();
					}
				}
				return true;
			}
		});
	}

	// 显示楼层滚轮
	public void showFloorPick() {
		flag = 3;
		mMenuView = inflater.inflate(R.layout.layout_wheel_time, null);
		btn_cancel = (Button) mMenuView.findViewById(R.id.wheel_cancel);
		btn_cancel.setOnClickListener(this);
		btn_ok = (Button) mMenuView.findViewById(R.id.wheel_ok);
		btn_ok.setOnClickListener(this);
		contentText = (TextView) mMenuView.findViewById(R.id.wheel_content);

		final WheelView floor1 = (WheelView) mMenuView.findViewById(R.id.hour);
		floor1.setViewAdapter(new NumericWheelAdapter(context, 1, 99));
		// hours.setLabel("hours");

		final WheelView floor2 = (WheelView) mMenuView.findViewById(R.id.mins);
		floor2.setViewAdapter(new NumericWheelAdapter(context, 1, 99));
		// mins.setLabel("mins");

		String textDate = fabuUpdateActivity.text_floor.getText().toString();
		if (textDate != null && !"".equals(textDate)) {
			String[] s1 = textDate.split("/");
			contentText.setText(s1[0] + "/" + s1[1]);
			floor1.setCurrentItem(Integer.parseInt(s1[0]));
			floor2.setCurrentItem(Integer.parseInt(s1[1]));
		} else {
			// set current time
			floor1.setCurrentItem(0);
			floor2.setCurrentItem(0);
			contentText.setText(1 + "/" + 1);
		}

		floor1.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// wheel.setLabel(newValue != 1 ? "mins" : "min");
				String[] s = contentText.getText().toString().split("/");
				if (wheel.getCurrentItem() > Integer.parseInt(s[1]) - 1) {
					s[0] = s[1];
					wheel.setCurrentItem(Integer.parseInt(s[1]) - 1);
				}
				contentText.setText((wheel.getCurrentItem() + 1) + "/" + s[1]);
			}
		});
		floor2.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// wheel.setLabel(newValue != 1 ? "hours" : "hour");
				String[] s = contentText.getText().toString().split("/");
				if (wheel.getCurrentItem() < Integer.parseInt(s[0]) - 1) {
					s[0] = (wheel.getCurrentItem() + 1) + "";
					floor1.setCurrentItem(Integer.parseInt(s[0]) - 1);
				}
				contentText.setText(s[0] + "/" + (wheel.getCurrentItem() + 1));
			}
		});

		this.setContentView(mMenuView);
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		this.setFocusable(true);
		this.setAnimationStyle(R.style.AnimBottom);
		ColorDrawable dw = new ColorDrawable(0x00000000);
		this.setBackgroundDrawable(dw);
		mMenuView.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				int height = mMenuView.findViewById(R.id.wheel_container)
						.getTop();
				int y = (int) event.getY();
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (y < height) {
						dismiss();
					}
				}
				return true;
			}
		});
	}

	// 显示房型
	public void showTypePick() {
		flag = 4;
		mMenuView = inflater.inflate(R.layout.layout_wheel_type, null);
		btn_cancel = (Button) mMenuView.findViewById(R.id.wheel_cancel);
		btn_cancel.setOnClickListener(this);
		btn_ok = (Button) mMenuView.findViewById(R.id.wheel_ok);
		btn_ok.setOnClickListener(this);
		contentText = (TextView) mMenuView.findViewById(R.id.wheel_content);

		String textDate = fabuUpdateActivity.text_type.getText().toString();
		List<Integer> list = StringTools.getNumbers(textDate);
		if (textDate != null && !"".equals(textDate)&&list.size()==3) {
			select_shi = list.get(0);
			select_ting = list.get(1);
			select_wei = list.get(2);
		} else {
			select_shi = 0;
			select_ting = 0;
			select_wei = 0;
		}

		shi = (WheelView) mMenuView.findViewById(R.id.shi);
		shi.setViewAdapter(new NumericWheelAdapter(context, 0, 10));

		ting = (WheelView) mMenuView.findViewById(R.id.ting);
		ting.setViewAdapter(new NumericWheelAdapter(context, 0, 10));
		wei = (WheelView) mMenuView.findViewById(R.id.wei);
		wei.setViewAdapter(new NumericWheelAdapter(context, 0, 10));

		// set current item
		shi.setCurrentItem(select_shi);
		ting.setCurrentItem(select_ting);
		wei.setCurrentItem(select_wei);

		contentText.setText(select_shi + "室" + select_ting + "厅" + select_wei
				+ "卫");

		shi.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				select_shi = wheel.getCurrentItem();
				contentText.setText(select_shi + "室" + select_ting + "厅"
						+ select_wei + "卫");
			}
		});
		ting.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				select_ting = wheel.getCurrentItem();
				contentText.setText(select_shi + "室" + select_ting + "厅"
						+ select_wei + "卫");
			}
		});
		wei.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				select_wei = wheel.getCurrentItem();
				contentText.setText(select_shi + "室" + select_ting + "厅"
						+ select_wei + "卫");
			}
		});

		this.setContentView(mMenuView);
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		this.setFocusable(true);
		this.setAnimationStyle(R.style.AnimBottom);
		ColorDrawable dw = new ColorDrawable(0x00000000);
		this.setBackgroundDrawable(dw);
		mMenuView.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				int height = mMenuView.findViewById(R.id.wheel_container)
						.getTop();
				int y = (int) event.getY();
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (y < height) {
						dismiss();
					}
				}
				return true;
			}
		});
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.wheel_cancel:
			dismiss();
			break;
		case R.id.wheel_ok:
			switch (flag) {
			case 1:
				fabuUpdateActivity.text_areaqy.setText(contentText.getText()
						.toString());
				break;
			case 2:
				fabuUpdateActivity.text_direction.setText(contentText.getText()
						.toString());
				break;
			case 3:
				fabuUpdateActivity.text_floor.setText(contentText.getText()
						.toString());
				break;
			case 4:
				fabuUpdateActivity.text_type.setText(contentText.getText()
						.toString());
				break;

			default:
				break;
			}
			dismiss();
			break;
		}
	}
}